home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / disk / misc / reformat10.lha / ReFormat / ReFormat.c < prev    next >
C/C++ Source or Header  |  1993-04-30  |  7KB  |  279 lines

  1. /*
  2.    ReFormat.c
  3.  
  4.    Do a QUICK FORMAT on a drive, and keep the volume label.
  5.  
  6.    Written by:
  7.       Nickey MacDonald
  8.       i6t4@jupiter.sun.csd.unb.ca
  9.       April 29, 1993
  10.  
  11.    Written and compiled with SAS C V6.2.
  12.  
  13.    This code should give you a feel for what the FORMAT command must go
  14.    through, minus the code to do low level formatting of the floppy.
  15.    I thought, at the start, it would be a simple matter of calling three
  16.    or for functions at most...  *HA*  The OS designers sure proved me wrong!
  17.    Oh well..  it was a fun learning experience, and now I have example code
  18.    to work with if I ever have to do this again...
  19. */
  20.  
  21.  
  22. #include <exec/types.h>
  23. #include <exec/memory.h>
  24. #include <dos/dos.h>
  25.  
  26. #include <proto/exec.h>
  27. #include <proto/dos.h>
  28.  
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32.  
  33. #define PROGRAM_NAME    "ReFormat"
  34. #define PROGRAM_VERSION "1.0"
  35. #define PROGRAM_DATE    "(1993.04.29)"
  36.  
  37. /* I can forsee there ever being a device name or volume label larger */
  38. /* that 256 characters...  I sure hope not! */
  39. #define MAX_NAME_LEN 256
  40.  
  41.  
  42. /* So the VERSION command can know who we are */
  43. static char *VersionString="$VER: " PROGRAM_NAME " " PROGRAM_VERSION " " PROGRAM_DATE;
  44.  
  45.  
  46. /* Disable SAS C's built in CTRL-C handler */
  47. void __regargs __chkabort(void);
  48.  
  49. void __regargs __chkabort(void)
  50. {
  51. }
  52.  
  53.  
  54. int main(int argc, char *argv[])
  55. {
  56.    BPTR floppylock;
  57.    char buf[2];
  58.    char namebuf[MAX_NAME_LEN];
  59.    struct InfoData *idata; /* Must be longword aligned */
  60.    int rv=RETURN_ERROR;
  61.    int justinfo, infogiven;
  62.    int l;
  63.    LONG err;
  64.    int doreformat;
  65.    LONG argarray[4];
  66.    struct RDArgs *argstruct;
  67.    char device[MAX_NAME_LEN];
  68.    int promptmode;
  69.    struct DosList *dlist;
  70.  
  71.    /* Manual says to initialize with default values */
  72.    argarray[0]=0;
  73.    argarray[1]=0;
  74.    argarray[2]=0;
  75.    argarray[3]=0;
  76.  
  77.    /* Parse the command line arguments */
  78.    argstruct=ReadArgs("DEVICE=DRIVE/K,NOPROMPT/S,ABOUT/S,VERSION/S", argarray, NULL);
  79.  
  80.    /* There was/were error(s) in the command line */
  81.    if (argstruct == NULL)
  82.    {
  83.       err=IoErr();
  84.       PrintFault(err, PROGRAM_NAME);
  85.       return(RETURN_ERROR);
  86.    }
  87.  
  88.    promptmode=argarray[1];
  89.  
  90.    infogiven=0;
  91.  
  92.    /* VERSION */
  93.    if (argarray[3] != 0)
  94.    {
  95.       printf(PROGRAM_NAME ": Version " PROGRAM_VERSION " " PROGRAM_DATE "\n");
  96.       infogiven=1;
  97.    }
  98.  
  99.    /* ABOUT */
  100.    if (argarray[2] != 0)
  101.    {
  102.       printf(PROGRAM_NAME " was written by Nick MacDonald <i6t4@jupiter.sun.csd.unb.ca>\n");
  103.       infogiven=1;
  104.    }
  105.  
  106.    if ((char *)argarray[0] != NULL)
  107.    {
  108.       strcpy(device, (char *)argarray[0]);
  109.       justinfo=0;
  110.    }
  111.    else
  112.    {
  113.       if (infogiven == 0)
  114.       {
  115.          printf(PROGRAM_NAME ": required argument missing\n");
  116.          rv=RETURN_ERROR;
  117.       }
  118.       else
  119.       {
  120.          rv=RETURN_WARN;
  121.       }
  122.       justinfo=1;
  123.    }
  124.  
  125.    /* Free up the memory the system used on our behalf */
  126.    FreeArgs(argstruct);
  127.  
  128.    if (justinfo == 1)  return(rv);
  129.  
  130.    l=strlen(device);
  131.    if (device[l-1] != ':')
  132.    {
  133.       printf(PROGRAM_NAME ": %s is not a valid parameter.\n", device);
  134.       return(RETURN_ERROR);
  135.    }
  136.  
  137.    /* I don't think this code is necessary any more...  the check of the */
  138.    /* device list should be more encompassing than this anyway */
  139.    /* Check and make sure we're gonna work on a file system */
  140. /* if (! IsFileSystem(device))
  141.    {
  142.       printf(PROGRAM_NAME ":%s is not a file system.\n", device);
  143.       return(RETURN_ERROR);
  144.    }*/
  145.  
  146.    /* Make sure its a device and not an ASSIGN */
  147.    device[l-1]='\0';
  148.    dlist=LockDosList(LDF_DEVICES | LDF_READ);
  149.    dlist=FindDosEntry(dlist, device, LDF_DEVICES);
  150.    UnLockDosList(LDF_DEVICES | LDF_READ);
  151.    device[l-1]=':';
  152.  
  153.    if (dlist == NULL)
  154.    {
  155.       printf(PROGRAM_NAME ":  device (or volume) '%s' is not mounted.\n", device);
  156.       return(RETURN_ERROR);
  157.    }
  158.  
  159.    /* Should probably use PutStr() rather than printf() to make a smaller */
  160.    /* executable... */
  161.    /* PutStr("test of PutStr()\n"); */
  162.  
  163.    /* The structure has to be longword aligned, which AllocMem() will */
  164.    /* ensure. */
  165.    if (idata=AllocMem(sizeof(struct InfoData), MEMF_PUBLIC | MEMF_CLEAR))
  166.    {
  167.       /* Need a lock to get volume name and info */
  168.       if ((floppylock=Lock(device, ACCESS_READ)) != NULL)
  169.       {
  170.          doreformat=0;
  171.  
  172.          /* This is how we get the existing volume name so it can be */
  173.          /* passed to format. */
  174.          if (NameFromLock(floppylock, namebuf, MAX_NAME_LEN))
  175.          {
  176.             /* truncate the colon off the volume name */
  177.             l=strlen(namebuf);
  178.             if (namebuf[l-1] == ':')
  179.             {
  180.                namebuf[l-1]='\0';
  181.             }
  182.  
  183.             /* We're gonna use the filesystem type for Format() */
  184.             if (Info(floppylock, idata))
  185.             {
  186.                doreformat=1;
  187.             }
  188.             else
  189.             {
  190.                err=IoErr();
  191.                PrintFault(err, PROGRAM_NAME);
  192.             }
  193.          }
  194.          else
  195.          {
  196.             err=IoErr();
  197.             PrintFault(err, PROGRAM_NAME);
  198.          }
  199.  
  200.          /* Don't need that lock any more...  God help them if they */
  201.          /* change floppies at this stage of the game, well maybe not */
  202.          /* you can test yourself and see what you think... */
  203.          UnLock(floppylock);
  204.  
  205.          /* We know enuff about the floppy to continue, so lets do so */
  206.          if (doreformat == 1)
  207.          {
  208.             if (promptmode == 0)
  209.             {
  210.                /* Warn the poor user what he just asked for */
  211.                printf(PROGRAM_NAME ":  About to erase all information on\n");
  212.                printf("           volume %s in drive %s!\n", namebuf, device);
  213.                printf("           *** Hit CTRL-C followed by RETURN to abort...\n");
  214.                printf("           *** Hit RETURN to continue...\n");
  215.  
  216.                /* Wait for input */
  217.                fgets(buf, 2, stdin);
  218.             }
  219.  
  220.             /* Check for the CTRL-C break signal */
  221.             if (CheckSignal(SIGBREAKF_CTRL_C))
  222.             {
  223.                printf(PROGRAM_NAME ":  User abort.\n");
  224.             }
  225.             else
  226.             {
  227.                /* User didn't break, so away we go... */
  228.  
  229.                if (Inhibit(device, DOSTRUE))
  230.                {
  231.                   /* now inhibited... */
  232.  
  233.                   /* This should be the same as a FORMAT QUICK */
  234.                   if (Format(device, namebuf, idata->id_DiskType))
  235.                   {
  236.                      printf("ReFormat:  Volume %s was erased.\n", namebuf);
  237.                      rv=RETURN_OK;
  238.                   }
  239.                   else
  240.                   {
  241.                      err=IoErr();
  242.                      PrintFault(err, "ReFormat");
  243.                      printf("ReFormat:  Volume %s was not erased!\n", namebuf);
  244.                   }
  245.  
  246.                   /* Unhibit the drive ... */
  247.                   if (Inhibit(device, FALSE))
  248.                   {
  249.                   }
  250.                   else
  251.                   {
  252.                      err=IoErr();
  253.                      PrintFault(err, PROGRAM_NAME);
  254.                   }
  255.                }
  256.                else
  257.                {
  258.                   err=IoErr();
  259.                   PrintFault(err, PROGRAM_NAME);
  260.                }
  261.             }
  262.          }
  263.       }
  264.       else
  265.       {
  266.          err=IoErr();
  267.          PrintFault(err, PROGRAM_NAME);
  268.       }
  269.  
  270.       FreeMem(idata, sizeof(struct InfoData));
  271.    }
  272.    else
  273.    {
  274.       printf(PROGRAM_NAME ":  Could not AllocMem()!\n");
  275.    }
  276.  
  277.    return(rv);
  278. }
  279.